package cn.edu.pzhu.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.pzhu.pojo.FileMsg;
import cn.edu.pzhu.service.FileMsgService;
import cn.edu.pzhu.service.imp.FileMsgServiceImp;
import cn.edu.pzhu.util.PageUtil;

/**
 * Servlet implementation class CategorySearchServlet
 */
@WebServlet("/category")
public class CategorySearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CategorySearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String page = request.getParameter("page");
		String num = request.getParameter("num");
		String type = request.getParameter("type");
		String status = request.getParameter("state");
		
		String sessionType = (String) request.getSession().getAttribute("type");
		if (type != null) {
		    request.getSession().setAttribute("type", type);
		} else if (sessionType != null) {
		    // 如果type参数为空且session中的type属性不为空，使用session中的type
		    type = sessionType;
		} else {
			response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("<script>alert('请重新选择分类词条！');location.href='show.jsp'</script>");
            return;
		}
//		System.out.println(type);
		int p = 1, n = 10;
		if(page!=null) { //"-1"
			try {
				p = Integer.parseInt(page);
			} catch (Exception e) {
				p = 1;
			}
		}
		if(num!=null) {
			try {
				n = Integer.parseInt(num);
			} catch (Exception e) {
				n = 10;
			}
		}
		if("1".equals(status)) {
			List<FileMsg> list = (List<FileMsg>) request.getSession().getAttribute("prelist");
			List<FileMsg> pageList=PageUtil.splitList(list, p, n);//调用工具类切分列表
			request.getSession().setAttribute("list", pageList);
			
			String address="category";
			StringBuffer bar = PageUtil.createBarByState(list, p, n, address, status);
			request.getSession().setAttribute("bar", bar);
			request.getSession().setAttribute("num", num);
			response.sendRedirect("showcategory.jsp");
		}else {
			FileMsgService fms=new FileMsgServiceImp();
			List<FileMsg> list = fms.selectByCategories(type);//调用接口通过用户名查询文件
			request.getSession().setAttribute("prelist", list);
			List<FileMsg> pageList=PageUtil.splitList(list, p, n);//调用工具类切分列表
			request.getSession().setAttribute("list", pageList);
			
			String address="category";
			StringBuffer bar = PageUtil.createBarByState(list, p, n, address, status);
			request.getSession().setAttribute("bar", bar);
			request.getSession().setAttribute("num", num);
			response.sendRedirect("showcategory.jsp");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
